/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Theme_Builder
{
    public partial class EditFontCollection : UserControl
    {
        public EditFontCollection()
        {
            InitializeComponent();
        }

        public void SetDefaultUI()
        {
            this.listViewOptionalFonts.Items.Clear();
            this.textBoxCustomLanguage.Text = "";
            this.textBoxCustomFont.Text = "";
        }

        public void GetFonts( OfficeOpenXMLInterop.CT_FontCollection coll )
        {
            int supFontCount = this.listViewOptionalFonts.Items.Count;
            coll.font = new OfficeOpenXMLInterop.CT_SupplementalFont[supFontCount];
            for (int i = 0; i < supFontCount; i++)
            {
                OfficeOpenXMLInterop.CT_SupplementalFont supFont = new OfficeOpenXMLInterop.CT_SupplementalFont();
                supFont.script = this.listViewOptionalFonts.Items[i].SubItems[0].Text;
                supFont.typeface = this.listViewOptionalFonts.Items[i].SubItems[1].Text;

                coll.font[i] = supFont;
            }
        }

        public void SetFonts( OfficeOpenXMLInterop.CT_FontCollection coll )
        {
            this.listViewOptionalFonts.Items.Clear();
            if (coll.font == null)
                return;

            int supFontCount = coll.font.Length;
            for (int i = 0; i < supFontCount; i++)
            {
                ListViewItem item = new ListViewItem(coll.font[i].script);
                item.SubItems.Add(coll.font[i].typeface);
                this.listViewOptionalFonts.Items.Add(item);
            }

        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            this.buttonAddCustomFont.Enabled = this.textBoxCustomFont.Text.Length > 0 && this.textBoxCustomLanguage.Text.Length > 0;
        }

        private void buttonAddCustomFont_Click(object sender, EventArgs e)
        {
            if (this.listViewOptionalFonts.Items.ContainsKey(this.textBoxCustomLanguage.Text))
            {
                MessageBox.Show("You already have already defined a custom font in the specified language.", "Unable to create Custom Font", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Add the font to the listview
            ListViewItem item = new ListViewItem();
            item.Text = this.textBoxCustomLanguage.Text;
            item.Name = this.textBoxCustomLanguage.Text;
            item.SubItems.Add(this.textBoxCustomFont.Text);
            this.listViewOptionalFonts.Items.Add(item);

            // Clear out the text boxes
            this.textBoxCustomFont.Clear();
            this.textBoxCustomLanguage.Clear();
        }

        private void buttonEditCustomFont_Click(object sender, EventArgs e)
        {
            if (this.listViewOptionalFonts.SelectedItems.Count <= 0)
                return;

            ListViewItem item = this.listViewOptionalFonts.SelectedItems[0];

            EditCustomFont ecf = new EditCustomFont();
            ecf.Language = item.Text;
            ecf.Typeface = item.SubItems[1].Text;   // the second sub item is actually the sub item which holds the font

            if (ecf.ShowDialog(this) == DialogResult.OK)
            {
                item.Text = ecf.Language;
                item.SubItems[1].Text = ecf.Typeface;
            }
        }

        private void buttonRemoveCustomFont_Click(object sender, EventArgs e)
        {
            if (this.listViewOptionalFonts.SelectedItems.Count > 0)
                this.listViewOptionalFonts.Items.Remove(this.listViewOptionalFonts.SelectedItems[0]);
        }

        private void listViewOptionalFonts_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool fEnabled = (this.listViewOptionalFonts.SelectedItems.Count > 0);

            this.buttonEditCustomFont.Enabled = fEnabled;
            this.buttonRemoveCustomFont.Enabled = fEnabled;
        }

        private void listViewOptionalFonts_Leave(object sender, EventArgs e)
        {
            // If we're not removing or editing the font, drop focus
            if (!this.buttonRemoveCustomFont.Focused && !this.buttonEditCustomFont.Focused)
                this.listViewOptionalFonts.SelectedItems.Clear();
        }
    }
}
